﻿using System;
using NUnit.Framework;

namespace PropertyExpression.ControllableQuery.Test
{
    public partial class DemoTest
    {
        [SetUp]
        public void Init()
        {
            MainTest.ExecuteNonQuery(context => DeleteAll());
            var publicationTypeId = Guid.NewGuid();
            MainTest.ExecuteNonQuery(context => InsertPublicationType(context.CreateParam(publicationTypeId)));
            var publicationId = Guid.NewGuid();
            MainTest.ExecuteNonQuery(context => InsertPublication(context.CreateParam(publicationId), context.CreateParam(publicationTypeId)));
            MainTest.ExecuteNonQuery(context => InsertModelPublication(
                context.CreateParam(Guid.NewGuid()),
                context.CreateParam(publicationId),
                context.CreateParam("ModelCode1"),
                context.CreateParam("ModelSequence1")));
        }

        private static INonQuery InsertModelPublication(
            IParam<Guid> modelPublicationId, IParam<Guid> publicationId,
            IParam<string> modelCode, IParam<string> modelSequence)
        {
            return
(@"INSERT ModelPublication
        (
         ModelPublicationId,
         ModelCode,
         ModelSequence,
         PublicationId
        )
VALUES
        (
         " + modelPublicationId + @",
         " + modelCode + @",
         " + modelSequence + @",
         " + publicationId + @"
        )").ToNonQuery();
        }

        private static INonQuery InsertPublication(IParam<Guid> publicationId, IParam<Guid> publicationTypeId)
        {
            return
(@"INSERT Publication
        (
         PublicationId,
         Code,
         PublicationTypeId,
         FollowingPublicationId
        )
VALUES
        (
         " + publicationId + @",
         N'PublicationCode',
         " + publicationTypeId + @",
         NULL
        )").ToNonQuery();
        }

        private static INonQuery InsertPublicationType(IParam<Guid> publicationTypeId)
        {
            return
(@"INSERT INTO PublicationType
        (
         PublicationTypeId,
         Code,
         Description,
         Decimal1,
         Money1
        )
VALUES
        (
         " + publicationTypeId + @",
         0,
         N'Description',
         0,
         0
        )").ToNonQuery();
        }

        private static INonQuery DeleteAll()
        {
            return 
(@"DELETE FROM ModelPublication
DELETE FROM Publication
DELETE FROM PublicationType").ToNonQuery();
        }
    }
}
